package com.ssm.car.server.controller.camera;


import com.jee.ssm.common.support.Const;
import com.jee.ssm.common.utils.UUIDFactory;
import com.jee.ssm.common.web.json.Tip;
import com.jee.ssm.common.web.param.ParamMap;
import com.ssm.car.dao.model.Camera;
import com.ssm.car.dao.model.Parking;
import com.ssm.car.rpc.api.camera.CameraService;
import com.ssm.car.rpc.api.parking.ParkingService;
import com.ssm.upms.dao.model.Account;
import com.ssm.upms.log.AdminControllerLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
* 摄像头表管理 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/camera")
public class CameraController  {


    /**
     * 进入摄像头表添加页面 携带一个生成的id --> longId
     * @param model 返回的实体容器
     * @return 添加页面
     */
    @RequestMapping(value="/add")
    @RequiresPermissions("camera:add")
    public String add(ModelMap model, HttpServletRequest request){
        HttpSession session = request.getSession();
        Account account = (Account)session.getAttribute(Const.ACCOUNT);
        model.put("type",account.getType());
      /*  if (account.getType() == 6){
            Parking parking = parkingService.selectById(account.getInfoId());
            model.put("parking",parking);
        }*/
        model.put("longId", UUIDFactory.getStringId());
        return "manager/camera/add";
    }


    /**
     * 进入摄像头表编辑页面
     * @param model 返回摄像头表的容器
     * @param id 摄像头表id
     * @return 编辑页面
     */
    @RequestMapping(value="/edit")
    @RequiresPermissions("camera:edit")
    public String edit(HttpServletRequest request, ModelMap model, @RequestParam String id) {
        HttpSession session = request.getSession();
        Account account = (Account)session.getAttribute(Const.ACCOUNT);
        /*if (account.getType() == 6){
            Parking parking = parkingService.selectById(account.getInfoId());
            model.put("parking",parking);
        }*//*else{
            model.put("parking",parkingService.listAll(null));
        }*/
        model.put("type",account.getType());
        model.put("data",cameraService.selectById(id));
        return "manager/camera/edit";
    }


    /**
     * 摄像头表添加
     * @param camera 带id的摄像头表对象
     * @return 成功状态
     */
    @RequestMapping(value="/save")
    @ResponseBody
    @RequiresPermissions("camera:add")
    @AdminControllerLog(description = "添加摄像头表" )
    public Tip save(Camera camera)  {

        try {
            cameraService.insert(camera);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"添加失败！");
        }

    }


    /**
     * 根据 id 修改摄像头表
     * @param camera 带id的摄像头表对象
     * @return 成功状态
     */
    @RequestMapping(value="/update")
    @ResponseBody
    @RequiresPermissions("camera:edit")
    @AdminControllerLog(description = "修改摄像头表" )
    public Tip update(Camera camera) {

        try {
            cameraService.updateById(camera);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"修改失败！");
        }

    }


    /**
     * 根据 id 删除摄像头表
     * @param id 摄像头表id
     * @return 成功状态
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    @RequiresPermissions("camera:delete")
    @AdminControllerLog(description = "删除摄像头表" )
    public Tip delete(@RequestParam String id) {

        try {
            cameraService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"删除失败！");
        }

    }


    /**
     * 根据 id 列表批量删除
     * @param ids 摄像头表id List
     * @return 成功状态
     */
    @RequestMapping(value="/deleteByIds")
    @ResponseBody
    @RequiresPermissions("camera:delete")
    @AdminControllerLog(description = "批量删除摄像头表" )
    public Tip deleteByIds(@RequestParam("ids") List<String> ids) {

        try {
            cameraService.deleteByIds(ids);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"批量删除失败！");
        }

    }


    /**
     * 根据 id 查找摄像头表
     * @param id 摄像头表id
     * @return 摄像头表对象 json
     */
    @RequestMapping(value="/findJson")
    @ResponseBody
    @RequiresPermissions("camera:list")
    public Camera find(@RequestParam String id) {

        return cameraService.selectById(id);
    }


    /**
     * 获取摄像头表列表 获取全部 不分页
     * @param request 请求参数
     * @return 摄像头表列表页面
     */
    @RequestMapping(value="/all")
    @RequiresPermissions("camera:list")
    public String all(HttpServletRequest request, ModelMap modelMap) {

        return list(request,modelMap,1,0);
    }


    /**
     * 获取摄像头表列表 分页
     * @param request 请求参数
     * @param page 第几页
     * @param size 每页大小
     * @return 摄像头表列表页面
     */
    @RequestMapping(value="/list")
    @RequiresPermissions("camera:list")
    public String list(HttpServletRequest request, ModelMap modelMap, Integer page, Integer size) {
        HttpSession session= request.getSession();
        Account account= (Account) session.getAttribute(Const.ACCOUNT);
        ParamMap paramMap = new ParamMap(request);
        if(account.getType() == 6) {
            paramMap.put("parkingId", account.getInfoId());
        }
        modelMap.put("pageInfo",cameraService.list(paramMap,page,size));
        modelMap.putAll(new ParamMap(request));
        return "manager/camera/list";
    }


    //---------------------------- property -------------------------------

    @Resource
    private CameraService cameraService;
    @Resource
    private ParkingService parkingService;

}
